package org.amj.wn.sect;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.amj.wn.sect.SectWaqfeNau.SectProperty;



@SuppressWarnings("serial")
public class SectServiceOperations extends HttpServlet {

	enum Operation {
		CREATE, DELETE, MODIFY;

	}

	class OperationInfo {
		private final String memberCode;
		private final String passCode;
		private final String name;
		private final String jamaat;
		
		private final Operation operation;
		private OperationInfo(String memberCode, String passCode,
				String name, String jamaat, Operation operation) {
			super();
			this.memberCode = memberCode;
			this.passCode = passCode;
			this.name = name;
			this.jamaat = jamaat;
			this.operation = operation;
		}
		public String getMemberCode() {
			return memberCode;
		}
		public String getPassCode() {
			return passCode;
		}
		public String getName() {
			return name;
		}
		public String getJamaat() {
			return jamaat;
		}
		public Operation getOperation() {
			return operation;
		}

		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		OperationInfo opInfo = getOperationInfo(req);
		
		int r = 2;
		if(opInfo != null) {
			switch(opInfo.getOperation()) {
			case CREATE:
				if(!SectWaqfeNau.createOrUpdate(opInfo.memberCode, opInfo.passCode, opInfo.name, opInfo.jamaat))
					r = 3;
				break;
			case DELETE:
				SectWaqfeNau.delete(opInfo.memberCode);
				break;
			case MODIFY:
				if(!SectWaqfeNau.modify(opInfo.memberCode, opInfo.passCode, opInfo.name, opInfo.jamaat))
					r = 1;
				break;
			}
		}		
		
		resp.sendRedirect("/admin/menu.jsp?r=" + r);
	}

	private OperationInfo getOperationInfo(HttpServletRequest req) {
		String operation = req.getParameter("opt");
		
		Operation opr = null;
		try {
			opr = Operation.valueOf(operation);
		}catch(Exception exp) { return null;}
		

		return new OperationInfo(req.getParameter(SectProperty.memberCode.name()), 
				req.getParameter(SectProperty.passCode.name()), 
				req.getParameter(SectProperty.name.name()), 
				req.getParameter(SectProperty.jamaat.name()), 
				opr);
	}

}
